/*
 * Copyright (C) 2012 BARBOTIN Nicolas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GCCodeBuilder.h"

GCCodeBuilder::GCCodeBuilder(QString uri, QObject *p) :
    QObject(p)
{
    if(QFile::exists(uri))
        QFile::remove(uri);

    fle.setFileName(uri);
    fle.open(QIODevice::WriteOnly);
    ts.setDevice(&fle);
}

void GCCodeBuilder::newLine()
{
    ts << "\n";
}

void GCCodeBuilder::include(QString file, bool glob)
{
    ts << "#include ";
    if(glob)
        ts << "<";
    else
        ts << "\"";

    ts << file;
    if(glob)
        ts << ">";
    else
        ts << "\"";

    newLine();
}

void GCCodeBuilder::protectHeader(QString h)
{
    ts << "#ifndef ";
    ts << h;
    newLine();
    ts << "#define ";
    ts << h;
    newLine();
    newLine();
}

void GCCodeBuilder::endHeaderProtection()
{
    ts << "#endif";
    newLine();
}

void GCCodeBuilder::output(QString str)
{
    ts << "std::cout << ";
    string(str);
    ts << " << std::endl;";
    newLine();
}

void GCCodeBuilder::write(QString str, bool nl)
{
    ts << str;
    if(nl)
        newLine();
}

void GCCodeBuilder::cond(QString c, bool a) {
    write("if(" + c + ")", !a);
    if(a)
        write(" {");
}

void GCCodeBuilder::number(int i)
{
    ts << QString::number(i);
}

void GCCodeBuilder::number(float f)
{
    ts << QString::number(f);
}

void GCCodeBuilder::string(QString str)
{
    ts << "\"" + str + "\"";
}

void GCCodeBuilder::boolean(bool b)
{
    if(b)
        ts << "true";
    else
        ts << "false";
}

void GCCodeBuilder::save()
{
    fle.close();
}
